package com.recallgo.manage.web.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.recallgo.manage.entity.ItemParam;
import com.recallgo.manage.service.ItemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * <p>
 * 商品规则参数 前端控制器
 * </p>
 *
 * @author fendo
 * @since 2018-09-11
 */
@Controller
@RequestMapping("/rest/item/param")
public class ItemParamController {

    @Autowired
    private ItemParamService itemParamService;

    @RequestMapping(value = "{itemCatId}", method = RequestMethod.GET)
    public ResponseEntity<ItemParam> queryByItemCatId(@PathVariable("itemCatId") Long itemCatId){
        EntityWrapper<ItemParam> wrapper = new EntityWrapper<>();
        wrapper.where("item_cat_id = {0}", itemCatId);
        ItemParam itemParam = itemParamService.selectOne(wrapper);
        if(null == itemParam){
            // 404
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
        return ResponseEntity.ok(itemParam);
    }

    /**
     * 添加
     * @param itemCatId
     * @param paramData
     * @return
     */
    @RequestMapping(value = "{itemCatId}", method = RequestMethod.POST)
    public ResponseEntity<Void> saveItemParam(@PathVariable("itemCatId") Long itemCatId, @RequestParam("paramData") String paramData){
        itemParamService.saveItem(itemCatId, paramData);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


}

